<!-- 制具&消耗品出库作业：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList5" @findClicked="findData" @Launchform="Launchform5" />
    <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList1" @findClicked="findData6" @Launchform="Launchform3" />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <h1>待入库明细</h1>
      <Table
        @confirmChange="confirmChange1"
        @currentRow="currentRow"
        :tableHeader="tableHeader"
        :tableData="QueryDetailsToBeStoredMethodsTableDateList"
        :TableHeight="50"
        :outTableHeight="80"
      />
      <h1>入库明细资料</h1>
      <Table :tableHeader="tableHeader1" :tableData="QueryTheStorageDetailsMethodsTableDateList" :TableHeight="50" :outTableHeight="80" />
      <PageModule :PageData="{ pageSizeShow: true }" />
    </el-card>

    <!-- 指令材料 -->
    <el-drawer v-model="drawer3" :direction="'ttb'" size="small" :with-header="false">
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList2" @Launchform="Launchform2" @findClicked="findData1" />
      <div style="width: 60%; margin-top: 13px">
        <Table
          :isLoading="isLoading1"
          @currentRow1="currentRowList1"
          @currentRow="currentRowList"
          :tableHeader="tableHeader2"
          :tableData="SelectMaterialQueryMehtodsTableDateList.dtos"
          :TableHeight="40"
          :outTableHeight="70"
        />
        <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChangeTk" @pageChange="handleCurrentChangeTk" />
      </div>

      <h1 style="margin-top: 15px">物料资料建档</h1>
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList3" @Launchform="Launchform1" @findClicked="findData5" />
    </el-drawer>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import service from '@/utils/request'
import { selectList } from '@/plugins/mixin'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'FixtureConsumables'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref, onMounted } from 'vue'
import {
  JianStorageDetailsAdded,
  JianToBeDeleted,
  SelectMaterialNew,
  GeneratedMaterialCoding,
  JianMaterialDeletion,
  JianDetailsToBeStoredAreAdded,
  SelectMaterialQuery,
  GainClassification,
  GainVendor,
  GainDeptNmae,
  JianQueryTheStorageDetails,
  JianQueryDetailsToBeStored
} from '@/api/WarehouseDepartment/FactoryStorage/ConsumablesIn'
import type {
  StorageDetailsAddedParms,
  SelectMaterialNewPamrs,
  SelectMaterialQueryInterface,
  QueryTheStorageDetailsParms
} from '@/api/WarehouseDepartment/FactoryStorage/ConsumablesIn'
import { ElMessage } from 'element-plus'
import { ElMessageBox } from 'element-plus'
// 待入库明细查询
const QueryDetailsToBeStoredMethodsTableDateList = ref<any>([]) as any

const formList1 = ref([
  {
    type: 'input',
    inputPlaceholder: '入库单号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '入库原因',
    selectLabel: '',
    selectOptions: [
      {
        optionKey: 1,
        optionValue: '材料入库',
        optionLabel: '材料入库'
      },
      {
        optionKey: 2,
        optionValue: '盘盈调整',
        optionLabel: '盘盈调整'
      },
      {
        optionKey: 3,
        optionValue: '现金入库',
        optionLabel: '现金入库'
      }
    ]
  },
  {
    type: 'input',
    inputPlaceholder: '送货单号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商代码 厂商名称',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '入库日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '待入库明细删除'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
]) as any

const formList2 = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '分类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '删除'
  }
])

const formList3 = ref([
  {
    type: 'input',
    inputPlaceholder: '物料编号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '品名',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '单位',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '分类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '规格',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '颜色',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '清空'
  },
  {
    type: 'button',
    checkboxLabel: '生成物料编码'
  },
  {
    type: 'button',
    checkboxLabel: '增加'
  }
])

const formList5 = ref([
  {
    type: 'button',
    checkboxLabel: '选择材料'
  },
  {
    type: 'dateSingle',
    dateLabel: '入库日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '入库原因',
    selectLabel: '',
    selectOptions: [
      {
        optionKey: 1,
        optionValue: '材料入库',
        optionLabel: '材料入库'
      },
      {
        optionKey: 2,
        optionValue: '盘盈调整',
        optionLabel: '盘盈调整'
      },
      {
        optionKey: 3,
        optionValue: '现金入库',
        optionLabel: '现金入库'
      }
    ]
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '部门',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商代码 厂商名称',
    selectLabel: '',
    selectOptions: []
  },

  {
    type: 'button',
    checkboxLabel: '入库批处理'
  },
  {
    type: 'button',
    checkboxLabel: '打印'
  }
]) as any

const findData6 = (count: string) => {
  if (count === '待入库明细删除') {
    if (ToBeDeletedParmsMtlNo.value === '') {
      JMessage.warning('请选择需要删除的数据')
    } else {
      handleCloseMateriaLKlcList()
    }
  }
  if (count === '置空') {
    clearFormList3()
  }
}

const clearFormList3 = () => {
  formList1.value.forEach((item: any) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
    if (item.type === 'dateSingle') {
      item.dateValue = ''
    }
  })

  formList5.value.forEach((item: any) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
    if (item.type === 'dateSingle') {
      item.dateValue = ''
    }
  })
}
// 待入库明细修改
const confirmChange1 = async (row: any) => {
  row.remark === null ? (row.remark = '') : row.remark
  const result = await LoginWork.post<any, any>(`/product/sdmaterial/updatePcSlipTempPart`, {
    amt: '',
    companyId: '',
    flag: '',
    item: '',
    lotNo: '',
    mtlColor: '',
    mtlName: '',
    mtlNo: row.mtlNo,
    mtlSpec: '',
    mtlUnit: '',
    price: '',
    purNo: '',
    qty: row.qty,
    remark: row.remark,
    slipType: '',
    stockNo: '',
    sysDate: '',
    sysUser: '',
    vendorName: '',
    vendorNo: ''
  })
  if (result.code === 200) {
    QueryDetailsToBeStoredMethods()
    JMessage.success(result.message)
  } else {
    QueryDetailsToBeStoredMethods()
    JMessage.error(result.message)
  }
}

// 入库明细资料新增
const Launchform5 = (row: any) => {
  StorageDetailsAddedMethodsParms.value.slipDate = row[1].dateValue
  StorageDetailsAddedMethodsParms.value.slipReason = row[2].selectValue
  StorageDetailsAddedMethodsParms.value.deptNo = row[3].selectValue.split(' ')[0]
  StorageDetailsAddedMethodsParms.value.deptName = row[3].selectValue.split(' ')[1]
  StorageDetailsAddedMethodsParms.value.vendorName = row[4].selectValue.split(' ')[1]
  StorageDetailsAddedMethodsParms.value.vendorNo = row[4].selectValue.split(' ')[0]
  StorageDetailsAddedMethodsParms.value.sysDate = row[5].dateValue
  StorageDetailsAddedMethodsParms.value.sendNo = row[6].inputValue
}

const StorageDetailsAddedMethodsParms = ref<any>({
  deptName: '',
  deptNo: '',
  mtlNo: '',
  sendNo: '',
  slipDate: '',
  slipReason: '',
  sysDate: '',
  vendorName: '',
  vendorNo: ''
})

const StorageDetailsAddedMethods = async () => {
  const result = await JianStorageDetailsAdded({
    ...StorageDetailsAddedMethodsParms.value,
    list: QueryDetailsToBeStoredMethodsTableDateList.value.map((item: any) => {
      return item.mtlNo
    })
  })
  if (result.code === 200) {
    QueryDetailsToBeStoredMethods()
    QueryTheStorageDetailsMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const ToBeDeletedParmsMtlNo = ref('')
// 待出剩料删除
const currentRow = (row: any) => {
  const { mtlNo } = row
  ToBeDeletedParmsMtlNo.value = mtlNo
  StorageDetailsAddedMethodsParms.value.mtlNo = mtlNo
}

const ToBeDeletedMethods = async () => {
  const result = await JianToBeDeleted(ToBeDeletedParmsMtlNo.value)
  if (result.code === 200) {
    ToBeDeletedParmsMtlNo.value = ''
    QueryDetailsToBeStoredMethods()
    JMessage.success(result.message)
  } else {
    QueryDetailsToBeStoredMethods()
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlcList = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      ToBeDeletedMethods()
    })
    .catch(() => {})
}

// 选择材料新增
const SelectMaterialNewMehtodsParms = ref<SelectMaterialNewPamrs>({
  mtlColor: '',
  mtlName: '',
  mtlNo: '',
  mtlSpec: '',
  mtlUnit: '',
  typeName: '',
  typeNo: ''
})

const SelectMaterialNewMehtods = async () => {
  const result = await SelectMaterialNew(SelectMaterialNewMehtodsParms.value)
  if (result.code === 200) {
    SelectMaterialQueryMehtods()
    JMessage.success(result.message)
  } else {
    SelectMaterialQueryMehtods()
    JMessage.error(result.message)
  }
}

// 生成物料编码
const clearFormList1 = () => {
  formList3.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
  })
}

const mtlTypeList = ref('')

const findData5 = (count: string) => {
  if (count === '生成物料编码') {
    GeneratedMaterialCodingMethods()
  }
  if (count === '清空') {
    clearFormList1()
  }
  if (count === '增加') {
    if (
      SelectMaterialNewMehtodsParms.value.mtlName === '' ||
      SelectMaterialNewMehtodsParms.value.mtlNo === '' ||
      SelectMaterialNewMehtodsParms.value.mtlColor === ''
    ) {
      JMessage.warning('物料编号物料名称颜色不可为空')
    } else {
      SelectMaterialNewMehtods()
    }
  }
}

const Launchform1 = (row: any) => {
  SelectMaterialNewMehtodsParms.value.mtlNo = row[0].inputValue
  SelectMaterialNewMehtodsParms.value.mtlName = row[1].inputValue
  SelectMaterialNewMehtodsParms.value.mtlUnit = row[2].inputValue

  SelectMaterialNewMehtodsParms.value.typeNo = row[3].selectValue.split(' ')[0]
  SelectMaterialNewMehtodsParms.value.mtlSpec = row[4].inputValue
  SelectMaterialNewMehtodsParms.value.mtlColor = row[5].inputValue
  SelectMaterialNewMehtodsParms.value.typeName = row[3].selectValue.split(' ')[1]
  mtlTypeList.value = row[3].selectValue.split(' ')[0]
}

const GeneratedMaterialCodingMethods = async () => {
  if (mtlTypeList.value === '') {
    JMessage.warning('请选择分类在进行生成')
  } else {
    const result = await GeneratedMaterialCoding(mtlTypeList.value)
    if (result.code === 200) {
      formList3.value[0].inputValue = result.data
      JMessage.success('写入成功')
    } else {
      JMessage.error('写入失败')
    }
  }
}

// 选择材料删除
const currentRowList = (row: any) => {
  MaterialDeletionMethodsParms.value = row.mtlNo
}

const MaterialDeletionMethodsParms = ref('')

const MaterialDeletionMethods = async () => {
  const result = await JianMaterialDeletion(MaterialDeletionMethodsParms.value)
  if (result.code === 200) {
    SelectMaterialQueryMehtods()
    MaterialDeletionMethodsParms.value = ''
    JMessage.success(result.message)
  } else {
    SelectMaterialQueryMehtods()
    MaterialDeletionMethodsParms.value = ''
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlc = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(async () => {
      MaterialDeletionMethods()
    })
    .catch(() => {})
}

// 入库明细新增
const currentRowList1 = (row: any) => {
  const { mtlNo } = row
  DetailsToBeStoredAreAddedMehtodsParms.value = mtlNo
  DetailsToBeStoredAreAddedMehtods()
}

const DetailsToBeStoredAreAddedMehtodsParms = ref('')

const DetailsToBeStoredAreAddedMehtods = async () => {
  const result = await JianDetailsToBeStoredAreAdded(DetailsToBeStoredAreAddedMehtodsParms.value)
  if (result.code === 200) {
    QueryDetailsToBeStoredMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 选择材料查询
const clearFormList = () => {
  formList2.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
  })
}

const findData1 = (row: any) => {
  if (row === '置空') {
    clearFormList()
  }
  if (row === '删除') {
    if (MaterialDeletionMethodsParms.value === '') {
      JMessage.warning('请选择需要删除的数据')
    } else {
      handleCloseMateriaLKlc()
    }
  }
}

const Launchform2 = (row: any) => {
  SelectMaterialQueryMehtodsParmsList.value.mtlName = row[1].inputValue
  SelectMaterialQueryMehtodsParmsList.value.type = row[0].selectValue.split(' ')[0]
  SelectMaterialQueryMehtods()
}

const isLoading1 = ref(false)

const SelectMaterialQueryMehtodsTableDateList = ref<SelectMaterialQueryInterface>({
  dtos: []
})

const SelectMaterialQueryMehtodsParms = ref({
  pageNum: 1,
  pageSize: 40
})

const SelectMaterialQueryMehtodsParmsList = ref({
  mtlName: '',
  type: ''
})

const SelectMaterialQueryMehtods = async () => {
  isLoading1.value = true
  const result = await SelectMaterialQuery(
    SelectMaterialQueryMehtodsParmsList.value.mtlName,
    SelectMaterialQueryMehtodsParmsList.value.type,
    SelectMaterialQueryMehtodsParms.value
  )
  SelectMaterialQueryMehtodsTableDateList.value.dtos = result.dtos
  total.value = result.total!
  isLoading1.value = false
}

const total = ref(0)

const handleSizeChangeTk = (val: number) => {
  SelectMaterialQueryMehtodsParms.value.pageSize = val
  SelectMaterialQueryMehtods()
}

const handleCurrentChangeTk = (val: number) => {
  SelectMaterialQueryMehtodsParms.value.pageNum = val
  SelectMaterialQueryMehtods()
}

// 获取分类
const GainClassificationMehtods = async () => {
  let NewArray = []
  const result = await GainClassification()
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.typeNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.typeNo + ' ' + item.typeName)
    }
    return arr
  }, [])
  formList2.value[0].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
  formList3.value[3].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
}

// 部门获取
const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

const GainDeptNmaeMethods = async () => {}

// 入库明细查询
const Launchform3 = (row: any) => {
  QueryTheStorageDetailsMethodsParms.value.slipNo = row[0].inputValue
  QueryTheStorageDetailsMethodsParms.value.slipReason = row[1].selectValue
  QueryTheStorageDetailsMethodsParms.value.sendNo = row[2].inputValue
  QueryTheStorageDetailsMethodsParms.value.vendorNo = row[3].selectValue.split(' ')[0]
  QueryTheStorageDetailsMethodsParms.value.mtlName = row[4].inputValue
  QueryTheStorageDetailsMethodsParms.value.slipDateEnd = row[5].dateValue[1]
  QueryTheStorageDetailsMethodsParms.value.slipDateStart = row[5].dateValue[0]
  QueryTheStorageDetailsMethods()
}

const QueryTheStorageDetailsMethodsParms = ref<QueryTheStorageDetailsParms>({
  lotNo: '',
  mtlName: '',
  sendNo: '',
  slipDateEnd: '',
  slipDateStart: '',
  slipNo: '',
  slipReason: '',
  sysDateEnd: '',
  sysDateStart: '',
  vendorNo: ''
})

const QueryTheStorageDetailsMethodsTableDateList = ref([])

const QueryTheStorageDetailsMethods = async () => {
  const result = await JianQueryTheStorageDetails(QueryTheStorageDetailsMethodsParms.value)
  QueryTheStorageDetailsMethodsTableDateList.value = result
}

const QueryDetailsToBeStoredMethods = async () => {
  const result = await JianQueryDetailsToBeStored()
  QueryDetailsToBeStoredMethodsTableDateList.value = result.map((item: any) => {
    item.change = 'Y'
    return item
  })
}

// 选择材料
const drawer3 = ref(false)

const findData = (count: any) => {
  if (count === '选择材料') {
    GainClassificationMehtods()
    SelectMaterialQueryMehtods()
    drawer3.value = true
  }
  if (count === '入库批处理') {
    if (
      StorageDetailsAddedMethodsParms.value.deptName === '' ||
      StorageDetailsAddedMethodsParms.value.vendorName === '' ||
      StorageDetailsAddedMethodsParms.value.slipReason === ''
    ) {
      JMessage.warning('数据输入不完整')
    } else {
      StorageDetailsAddedMethods()
    }
  }
}

const ObtainDataOnTheFirstScreen = async () => {
  QueryDetailsToBeStoredMethods()

  QueryTheStorageDetailsMethods()

  let NewArray = []
  const result = await GainDeptNmae()
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.deptName && item.deptNo) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.deptNo + ' ' + item.deptName)
    }
    return arr
  }, [])
  formList5.value[3].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
}

onMounted(() => {
  ObtainDataOnTheFirstScreen()
})

const getSelectList = async () => {
  await selectList(formList5, () => service.get<any, any>(`/prod/getPcProdDynamicCondition`))
  let vendorNames = formList5.value.find(
    (item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
  ) as any
  let vendorNames2 = formList1.value.find(
    (item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
  ) as any
  let vendorName = await service.get<any, any>('/back/vendorQuery')
  if (vendorName && vendorNames) {
    vendorName.forEach((item: any, index: any) => {
      if (item === null) return
      vendorNames.selectOptions?.push({
        optionKey: index,
        optionValue: `${item.vendorNo} ${item.vendorName}`
      })
      vendorNames2.selectOptions?.push({
        optionKey: index,
        optionValue: `${item.vendorNo} ${item.vendorName}`
      })
    })
  }
}
onMounted(() => {
  getSelectList()
})

// 表格数据
const tableHeader = ref([
  {
    label: '材料颜色',
    prop: 'mtlColor',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    change: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },
  {
    label: '数量',
    prop: 'qty'
  },
  {
    label: '物料编码',
    prop: 'mtlNo',
    change: true
  },
  {
    label: '厂商代码',
    prop: 'vendorNo',
    change: true
  },
  {
    label: '订购单号',
    prop: 'purNo',
    change: true
  },
  {
    label: '厂商名称',
    prop: 'vendorName',
    change: true
  },
  {
    label: '备注',
    prop: 'remark'
  }
])

const tableHeader1 = ref([
  {
    label: '入库单号',
    prop: 'slipNo'
  },
  {
    label: '入库日期',
    prop: 'slipDate'
  },
  {
    label: '入库原因',
    prop: 'slipReason'
  },
  {
    label: '厂商',
    prop: 'vendorName'
  },
  {
    label: '材料颜色',
    prop: 'slipDate'
  },
  {
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '数量',
    prop: 'qty'
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '金额',
    prop: 'amt'
  },

  {
    label: '送货日期',
    prop: 'sysDate'
  },
  {
    label: '物料编号',
    prop: 'mtlNo'
  },
  {
    label: '部门名称',
    prop: 'deptName'
  },
  {
    label: '送货单号',
    prop: 'sendNo'
  },
  {
    label: '厂商',
    prop: 'vendorNo'
  },
  {
    label: '仓管员',
    prop: 'sysUser'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])

const tableHeader2 = ref([
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '分类名称',
    prop: 'typeName'
  },
  {
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '颜色',
    prop: 'mtlColor'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  }
])
</script>

<style lang="less" scoped>
h1 {
  font-size: 16px;
}
</style>
